# -*- coding: utf-8 -*-
import json

from django.conf import settings as conf
from django.http import HttpResponse
from django.views import generic
from braces.views import JSONResponseMixin

from institutes.models import DailyCounsel, DailyMeeting

# 券商每日发布信息JSON展现

class DailyCounselApiView(generic.View):
    
    def get(self, *args, **kwargs):
        date = self.request.GET.get('date', '')
        if date != '':
            daily_counsels = DailyCounsel.objects.filter(date=date)
        if daily_counsels.count() == 0:
            daily_counsels = DailyCounsel.objects.order_by('-date')[:conf.API_DEFAULT_MAX_NUM_RESULTS]
        daily_counsel_list = [{'Date': counsel.date.strftime('%Y-%m-%d'),
                               'Institute': counsel.institute,
                               'Author': counsel.author, 
                               'Content': counsel.content,
                              } for counsel in daily_counsels]
        return HttpResponse(json.dumps(daily_counsel_list))

class DailyMeetingApiView(JSONResponseMixin, generic.View):
    
    def get(self, *args, **kwargs):
        date = self.request.GET.get('date', '')
        if date != '':
            daily_meetings = DailyMeeting.objects.filter(date=date)
        if daily_meetings.count() == 0:
            daily_meetings = DailyMeeting.objects.order_by('-date')[:conf.API_DEFAULT_MAX_NUM_RESULTS]
        daily_meeting_list = [{'Date': meeting.date.strftime('%Y-%m-%d'),
                               'Institute': meeting.institute.short_name,
                               'StaticSummary': meeting.static_summary, 
                               'Url': meeting.url,
                               'SecName': meeting.stock.short_name,
                              } for meeting in daily_meetings]
        return HttpResponse(json.dumps(daily_meeting_list))

